import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import departments from './modules/departments'
import settings from './modules/settings'
import employees from './modules/employees'
import permissions from './modules/permissions'
import salarys from './modules/salarys'
import social_securitys from './modules/social_securitys'
import attendances from './modules/attendances'
import approvals from './modules/approvals'

// asyncRoutes 动态路由: 这里的路由都和权限有关
export const asyncRoutes = [
  departments,
  settings,
  employees,
  permissions,
  salarys,
  social_securitys,
  attendances,
  approvals
]

// es6 的新特性: 合并数组的方法
// ... 展开运算符
// arr.concat(arr2)
// arr.push(...arr2) push 的参数是一个个的数字, 他的返回值是新数组长度

// constantRoutes 静态路由: 不需要登录就可以访问的路由, 如: 首页, 登录, 注册, 404等等
export const constantRoutes = [
  // 静态: 登录页
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  // 静态: 404
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 静态: 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard'),
        meta: { title: '主页', icon: 'dashboard' }
      }
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 合并路由
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
